package com.chang.rentmall.controllers;

import com.chang.rentmall.beans.MessageBean;
import com.chang.rentmall.beans.TokenBean;
import com.chang.rentmall.services.TokenServices;
import com.chang.rentmall.utils.HttpRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class SystemController {
    @Value("${weixin.appId}")
    private String appId;
    @Value("${weixin.secret}")
    private String secret;
    @Autowired
    private TokenServices tokenServices;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = "/token/get")
    public String getToken (HttpServletRequest request, HttpServletResponse response) {
        String url = "https://api.weixin.qq.com/cgi-bin/token";
        String params ="grant_type=client_credential&appid="+ appId +"&secret=" + secret;
        String result = HttpRequest.sendGet(url,params);
        logger.info("获取Token---");

        ObjectMapper mapper = new ObjectMapper();

        try {
            TokenBean bean = mapper.readValue(result, TokenBean.class);
            HttpSession session = request.getSession();
            session.setAttribute("token", bean.getAccess_token());
            logger.info(bean.getAccess_token());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/getToken")
    public TokenBean getToken(){
        TokenBean bean = tokenServices.getTocken();
        return bean;
    }

    @RequestMapping(value = "/getUserOpenId", method = RequestMethod.POST)
    public MessageBean getOpenId(@RequestParam("code") String code, HttpServletResponse response) throws JsonProcessingException {
        MessageBean messageBean = new MessageBean();
        ObjectMapper mapper = new ObjectMapper();
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        String params = "appid="+ appId + "&secret="+ secret +"&js_code="+ code +"&grant_type=authorization_code";
        System.out.println("请求opneid ------------->");
        System.out.println(url + "?" + params);
        System.out.println("-------------------------");
        String resultStr = HttpRequest.sendGet(url, params);
        TokenBean bean = mapper.readValue(resultStr, TokenBean.class);
        messageBean.setValues(200, "获取openid成功", bean);
        return messageBean;
    }
}